/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 100;

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    bool flag = true;
    int cnt1 = 1, cnt2 = n * n;
    for (int i = 1; i <= n; ++i) {
      if (i & 1) {
        for (int j = 1; j <= n; ++j) {
          cout << (flag ? cnt1++ : cnt2--) << ' ';
          flag = !flag;
        }
      } else {
        std::vector<int> tmp;
        for (int j = n; j >= 1; --j) {
          tmp.push_back(flag ? cnt1++ : cnt2--);
          flag = !flag;
        }
        std::reverse(tmp.begin(), tmp.end());
        for (auto i : tmp) {
          cout << i << ' ';
        }
      }
      cout << '\n';
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("B. Matrix of Differences.in", "r", stdin);
  freopen("B. Matrix of Differences.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
